<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               ref="crud"
               :before-open="beforeOpen"
               v-model="form"
               :permission="permissionList"
               :page.sync="page"
               @search-change="searchChange"
               @search-reset="searchReset"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
    </avue-crud>
  </basic-container>
</template>

<script>
  import {mapGetters} from "vuex";
  import {getErrorList, getErrorLogs} from "@/api/system/logs";
  import {getDictionaryAPI} from '@/api/system/dict'
  import {searchDateRange} from "@/util/util";
  
  export default {
    data() {
      return {
        form: {},
        selectionList: [],
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        option: {
          height: 'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 5,
          searchGutter: 10,
          border: true,
          index: true,
          viewBtn: true,
          editBtn: false,
          addBtn: false,
          delBtn: false,
          menuWidth: 120,
          dialogType: 'drawer',
          searchLabelPosition: 'top',
          searchIcon: true,
          searchIndex: 3,
          column: [
            {
              label: "服务ip",
              prop: "serverIp",
              width:'160',
              search: true,
              searchLabel: '',
              searchPlaceholder: '服务ip',
            },
            {
              label: "访问者ip",
              prop: "visitorIp",
              width:'160',
              search: true,
              searchLabel: '',
              searchPlaceholder: '访问者ip',
            },
            {
              label: "系统环境",
              prop: "env",
              type: "select",
              dicUrl: getDictionaryAPI +'?code=env',
              dataType: 'number',
              props:{
                label: 'dictText',
                value: 'dictValue'
              },
              width: 70,
              search: true,
              searchLabel: '',
              searchPlaceholder: '系统环境',
            },
            {
              label: "请求方法",
              prop: "method",
              width:'80'
            },
            {
              label: "请求接口",
              prop: "url"
            },
            {
              label: "日志时间",
              prop: "createTime",
              type: "datetime",
              format: "yyyy-MM-dd HH:mm:ss",
              valueFormat: "yyyy-MM-dd HH:mm:ss",
              searchRange:true,
              searchProp: 'createTimeRange',
              search: true,
              searchLabel: '',
              searchPlaceholder: '系统环境',
              width:'180',
            },
            {
              label: "用户代理",
              prop: "userAgent",
              span: 24,
              hide: true
            },
            {
              label: "请求数据",
              prop: "data",
              type: "textarea",
              span: 24,
              minRows: 2,
              hide: true
            },
            {
              label: "异常类",
              prop: "exceptionClass",
              span: 24,
              hide: true,
            },
            {
              label: "错误信息",
              prop: "message",
              type: "textarea",
              span: 24,
              minRows: 16,
              hide: true
            }
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          viewBtn: this.vaildData(this.permission.log_error_view, false)
        };
      }
    },
    methods: {
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        // 时间范围查询时格式转换
        params = searchDateRange(this.option.column, params)
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      async beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          const { data } = await getErrorLogs(this.form.id)
          this.form = data;
        }
        done();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      async onLoad(page, params = {}) {
        this.loading = true;
        const { data } = await getErrorList(page.currentPage, page.pageSize, Object.assign(params, this.query))
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      }
    }
  };
</script>

<style>
</style>
